﻿using Bilibili.Model.Article;
using Bilibili.Model.Resource;
using Newtonsoft.Json.Linq;
using PCLUntils.Json;
using System;
using System.Threading.Tasks;

namespace Bilibili.WebClient.Api
{
    public sealed class ArticleClient : ClientBase
    {
        public async Task<ReturnModel<JObject>> GetCategories()
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo("https://api.bilibili.com/x/article/categories", false);
                var results = await ApiClient.GetResults(apiInfo);
                var obj = results.ToJObject();
                return BuildSuccessResult(obj);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<JObject>(ex);
            }
        }
        public async Task<ReturnModel<Article>> GetHome(int pageIndex)
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo("https://api.bilibili.com/x/article/home", $"&cid=0&pn={pageIndex}&ps=20", false);
                var results = await ApiClient.GetResults(apiInfo);
                Article m = results.ParseObject<Article>();
                return BuildSuccessResult(m);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<Article>(ex);
            }
        }
        public async Task<ReturnModel<JObject>> GetRecommends(int cid, int pageIndex)
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo("https://api.bilibili.com/x/article/recommends", $"&cid={cid}&from=2&pn={pageIndex}&ps=20&sort=0", true);
                string re = await ApiClient.GetResults(apiInfo);
                var obj = re.ToJObject();
                return BuildSuccessResult(obj);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<JObject>(ex);
            }
        }
    }
}